<template>
  <div class="common-layout">
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/img/avatar.jpg" alt="" />
          <span>电商平台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>

      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width=" !isCollapse ? '200px' : '64px'">
            <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 侧边栏导航区域 -->
          <el-menu
            active-text-color="#409EFF"
            background-color="#545c64"
            class="el-menu-vertical-demo"
            :default-active="activePath"
            text-color="#fff"
            @open="handleOpen"
            @close="handleClose"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router  
          >
            <el-sub-menu
              :index="item.id + ''"
              v-for="item of menulist"
              :key="item.id"
            >
              <!-- 一级菜单 -->
              <template #title>
                <i class="iconfont" :class="menuIcon[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                v-for="subitem of item.children"
                :key="subitem.id"
                :index=" '/' + subitem.path"
                v-show="item.children.length > 0"
                @click="savaNavState('/' + subitem.path)"
              >
                <span>{{ subitem.authName }}</span>
              </el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主体 -->
        <el-main>
          <el-config-provider :locale="zhCn">
            <router-view></router-view>
          </el-config-provider>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import zhCn from "element-plus/lib/locale/lang/zh-cn" 


import axios from '../http/axios.js'

export default {
  name: 'home',
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath') || '';
  },
  data() {
    return {
      menulist: [],
      menuIcon: {
        '125': 'icon-users',
        '103': 'icon-tijikongjian',
        '101': 'icon-shangpin',
        '102': 'icon-danju',
        '145': 'icon-baobiao'
      },
      isCollapse: false,
      activePath: '',
      zhCn: zhCn
    }
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await axios.get('menus')
      if (res.meta.status !== 200) {
        this.$message({
          type: 'error',
          message: res.meta.msg,
        })
      } else {
        this.menulist = res.data
      }
    },
    handleOpen() {},
    handleClose() {},
    // 是否折叠侧边栏
    toggleCollapse(){
        this.isCollapse = !this.isCollapse;
    },
    // 保存连接的激活状态
    savaNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath);
        this.activePath = activePath;
    }
  },
}
</script>

<style lang="less" scoped>
.iconfont{
    padding-right: 10px;
}

.el-menu{
    border-right: none;
}

.common-layout {
  height: 100%;
}

.toggle-button{
    color: #fff;
    text-align: center;
    word-spacing: .2rem;
    cursor: pointer;
}

.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  > div {
    height: 100%;
    display: flex;
    align-items: center;

    img {
      height: 100%;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}
</style>